<?php

class ProviderController extends BaseController {
    
    public function getList($shop_id){
        
        try{
            if($shop_id = Input::get('shop_id')){
                echo json_encode(Provider::ListProvider($shop_id)); 
            }
        }
        catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        } 
        
      }
      
      
    public static function postRegister(){

        try{
            $input=Input::json()->all();

            $shop_id=InputHelper::getInput($input,'shop_id');

            $name=InputHelper::getInput($input,'name');

            $address=InputHelper::getInput($input,'address');

            $phone=InputHelper::getInput($input,'phone','');

            $website=InputHelper::getInput($input,'website','');
            if(!empty($website) && !filter_var($website, FILTER_VALIDATE_URL))
            {
                return ErrorResponse::SendErrorMessage('website address invalid');
            }
            $email=InputHelper::getInput($input,'email','');
            if(!empty($website) && !filter_var($website, FILTER_VALIDATE_EMAIL))
            {
                return ErrorResponse::SendErrorMessage('email address invalid');
            }
            $comments=InputHelper::getInput($input,'comments','');
        }catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        } 
        try{
            
            if(Provider::Insert($shop_id,$name, $address, $phone, $website, $email, $comments))
            {
                return Response::json(array('status'=>'success'));
        
            }
                        
        }
        catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        }
     }
     
     
     public static function postEdit(){
       
            $input=Input::json()->all();

            $provider_id=InputHelper::getInput($input,'provider_id');

            $name=InputHelper::getInput($input,'name');

            $address=InputHelper::getInput($input,'address');

            $phone=InputHelper::getInput($input,'phone','');

            $website=InputHelper::getInput($input,'website','');
            if(!empty($website) && !filter_var($website, FILTER_VALIDATE_URL))
            {
                return ErrorResponse::SendErrorMessage('website address invalid');
            }
            $email=InputHelper::getInput($input,'email','');
            if(!empty($website) && !filter_var($website, FILTER_VALIDATE_EMAIL))
            {
                return ErrorResponse::SendErrorMessage('email address invalid');
            }
            $comments=InputHelper::getInput($input,'comments','');
        try{
            
            if(Provider::Edit($name, $address, $phone, $website, $email, $comments,$provider_id))
            {
            
            return Response::json(array('status'=>'success'));    
                
            }
            
                       
        }
        catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        }
        
        
     }
      
      
       public function postDelete(){
            $input=Input::json()->all();

            $provider_id=InputHelper::getInput($input,'provider_id');
            try{
                
                if( Provider::Del($provide_id)){
                    
                    return Response::json(array('status'=>'success'));
                    
                }
                         
        }
        catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        } 
      }

 
  }
 
?>    
    